﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AcctrueWMS.Foundation.WebControls
{
    public class AccControl
    {
        public AccControl()
        {
            Visible = true;
            Enabled = true;
            Musted = false;
        }
        /// <summary>
        /// 控件ID
        /// </summary>
        public string ID
        {
            get;
            set;
        }
        /// <summary>
        /// 控件字段名
        /// </summary>
        public string Name
        {
            get;
            set;
        }
        /// <summary>
        /// 校验表达式
        /// </summary>
        public string ValidExpression
        {
            get;
            set;
        }

        /// <summary>
        /// 校验未通过提示语
        /// </summary>
        public string Tip
        {
            get;
            set;
        }

        /// <summary>
        /// 默认值
        /// </summary>
        public string DefaultValue
        {
            get;
            set;
        }
        public Data.IDataModelContext Context
        {
            get;
            set;
        }
        ////区分编辑模板的空间,还是修改,参看
        //public int OptType
        //{
        //    get;
        //    set;
        //}
        /// <summary>
        /// 绑定数据集
        /// </summary>
        public object DataSource
        {
            get;
            set;
        }
        /// <summary>
        /// title
        /// </summary>
        public string Title
        {
            get;
            set;
        }
        /// <summary>
        /// 是否必填项
        /// </summary>
        public bool Musted
        {
            get;
            set;
        }
        /// <summary>
        /// 是否必填项
        /// </summary>
        public string Length
        {
            get;
            set;
        }
        /// <summary>
        /// 是否可见
        /// </summary>
        public bool Visible
        {
            get;
            set;
        }
        /// <summary>
        /// 是否可编辑
        /// </summary>
        public bool Enabled
        {
            get;
            set;
        }
        /// <summary>
        /// 控件标题
        /// </summary>
        public string Description
        {
            get;
            set;
        }
        /// <summary>
        /// 控件的值
        /// </summary>
        public string SelectedValue
        {
            get;
            set;
        }
        /// <summary>
        /// 显示格式
        /// </summary>
        public string Format
        {
            get;
            set;
        }
        /// <summary>
        /// 控件的显示样式
        /// </summary>
        public string Style
        {
            get;
            set;
        }
        /// <summary>
        /// 控件的显示列
        /// </summary>
        public string ValueMember
        {
            get { return "Value"; }
        }
        /// <summary>
        /// 控件的value列
        /// </summary>
        public string KeyMember
        {
            get { return "Key"; }
        }

        /// <summary>
        /// 控件的附加信息成员
        /// </summary>
        public string TextMember
        {
            get { return "text"; }
        }
        public string Text
        {
            get;
            set;
        }
        /// <summary>
        /// URL FocusTextbox使用
        /// </summary>
        public string Url
        {
            get;
            set;
        }
        /// <summary>
        /// 列类型,int string ,decimal.....
        /// </summary>
        public string DataType
        {
            get;
            set;
        }
        ///// <summary>
        ///// example  textarea's width
        ///// </summary>
        //public string Width
        //{
        //    get;
        //    set;
        //}
        ///// <summary>
        ///// 高度
        ///// </summary>
        //public string Height
        //{
        //    get;
        //    set;
        //}

        /// <summary>
        /// 控件呈现
        /// </summary>
        /// <returns></returns>
        public virtual string Render()
        {

            return SelectedValue;
        }
        /// <summary>
        /// 构造基本属性
        /// </summary>
        /// <returns></returns>
        protected string GenerateBasicProperty()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("  id='" + this.ID + "' ");
            sb.Append("  name='" + this.ID + "' ");
            sb.Append(" ctitle='"+this.Title+"' ");
            if (!this.Visible)
            {
                sb.Append(" style='display:none' ");
            }
            if (!this.Enabled)
            {
                sb.Append(" readonly='readonly' style='background-color:#F7F8F0' ");
            }
            if (this.Musted)
            {
                sb.Append(" musted='true'");
            }
            if (!string.IsNullOrEmpty(this.ValidExpression))
            {
                sb.Append(" validExpression='" + ValidExpression + "'");
            }
            if (!string.IsNullOrEmpty(this.Property))
            {
                sb.Append(this.Property);
            }
            if (!string.IsNullOrEmpty(this.Length))
            {
                sb.Append(string.Format("length='{0}'",this.Length));
            }
            return sb.ToString();
        }
        /// <summary>
        /// 控件值
        /// </summary>
        /// <returns></returns>
        protected string GetControlValue()
        {
            string controlValue = string.Empty;
            if (!string.IsNullOrEmpty(this.SelectedValue))
            {
                controlValue = SelectedValue;
            }
            else
            {
                if (!string.IsNullOrEmpty(this.DefaultValue))
                {
                    controlValue = DefaultValue;
                }
            }
            return controlValue;

        }


        protected string GetJQueryID()
        {
            return GetJQueryObject(this.ID);
        }
        /// <summary>
        /// 获取JQueryID,如果有特殊字符 转义该特殊字符
        /// </summary>
        /// <returns></returns>
        public string GetJQueryObject(string id)
        {
            if (id.Contains("$"))
            {
                return id.Replace("$", "\\\\$");
            }
            else
            {
                return id;
            }
        }
        /// <summary>
        /// 属性栏
        /// </summary>
        public string Property
        {
            get;
            set;
        }


    }
}
